package com.daodai.hjcjhsystemtd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daodai.hjcjhsystemtd.mapper.PassageDeviceNodeMapper;
import com.daodai.hjcjhsystemtd.model.pojos.DeviceControlNode;
import com.daodai.hjcjhsystemtd.model.pojos.DictDeviceType;
import com.daodai.hjcjhsystemtd.model.pojos.PassageDeviceNode;
import com.daodai.hjcjhsystemtd.service.CacheInitService;
import com.daodai.hjcjhsystemtd.service.DeviceControlNodeService;
import com.daodai.hjcjhsystemtd.service.DictDeviceTypeService;
import com.daodai.hjcjhsystemtd.service.PassageDeviceNodeService;
import com.daodai.hjcjhsystemtd.utils.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.objenesis.instantiator.perc.PercInstantiator;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

@Service
public class CacheInitServiceImpl implements CacheInitService {

    @Autowired
    private PassageDeviceNodeService passageDeviceNodeService;

    @Autowired
    private CacheUtil cacheUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DictDeviceTypeService dictDeviceTypeService;

    @Autowired
    private DeviceControlNodeService deviceControlNodeService;
    /**
     * 初始化点位表，将其加载到缓存中
     */
    @Override
    public void initNodeTableToCache() {
        //1.事先先清除当前数据库中的所有数据
        redisTemplate.getConnectionFactory().getConnection().flushDb();
        Map<String, Map<String,String>> cacheInfo = new HashMap<>();
        //2.创建分页对象，每页500条
        int batchSize = 500;//每页数量
        int currentPage = 1;//当前页数
        while (true) {
            IPage<PassageDeviceNode> page = new Page<>(currentPage, batchSize);

            //3.查询点位信息，并将其写入到cache中
            List<PassageDeviceNode> records = passageDeviceNodeService.list(page);
            records.stream().forEach(passageDeviceNode -> {
                Map<String,String> map = new HashMap<>();
                map.put("passageDeviceNode",passageDeviceNode.getPassageDeviceNode());
                map.put("port",passageDeviceNode.getPort());
                map.put("deviceTypeId",passageDeviceNode.getDeviceTypeId());
                map.put("deviceTypeName",passageDeviceNode.getDeviceTypeName());
                map.put("nodeId",passageDeviceNode.getNodeId());
                map.put("nodeDescribe",passageDeviceNode.getNodeDescribe());
                map.put("deviceId",passageDeviceNode.getDeviceId());
                map.put("location",passageDeviceNode.getPassage());
                cacheInfo.put(passageDeviceNode.getSid().toString(),map);
            });

            //判断是否开始下一轮
            if (records==null || records.size()<500) {
                break;
            }else {
                currentPage++;
            }

        }
        //4.将每个hash表的key单独作为索引存在cache的list集合内
        Set<String> node_index = cacheInfo.keySet();
        for (String nodeIndex : node_index) {

            cacheUtil.hPutAll("nodeTable:node_index_" + nodeIndex,cacheInfo.get(nodeIndex));
            cacheUtil.lLeftPush("node_index","nodeTable:node_index_" + nodeIndex);
        }
    }

    /**
     * 初始化量程表，将其加载到缓存中
     */
    @Override
    public void initNodeRange() {
        //1.获取量程表信息
        List<DictDeviceType> dictDeviceTypes = dictDeviceTypeService.list();

        //2.将量程表信息存在cache的哈希表中
        for (DictDeviceType dictDeviceType : dictDeviceTypes) {
            Map<String,String> map = new HashMap<>();
            map.put("deviceTypeName",dictDeviceType.getDeviceTypeName());
            map.put("valueRange",dictDeviceType.getRangesVal());
            map.put("regular",dictDeviceType.getRegular());
            cacheUtil.hPutAll("rangeTable:"+ dictDeviceType.getDeviceTypeId(),map);
        }
    }


    /**
     * 初始化控制点位表，将其加载到缓存中
     */
    public void initControlTableToCache() {
//        //1.事先先清除当前数据库中的所有数据
//        redisTemplate.getConnectionFactory().getConnection().flushDb();
        Map<String, Map<String, String>> cacheInfo = new HashMap<>();
        //2.创建分页对象，每页500条
        int batchSize = 500;//每页数量
        int currentPage = 1;//当前页数
        while (true) {
            //3.查询点位信息，并将其写入到cache中
           IPage<DeviceControlNode> page = new Page<>(currentPage, batchSize);
            List<DeviceControlNode> list = deviceControlNodeService.list(page);
            list.stream().forEach(deviceControlNode -> {
                Map<String, String> map = new HashMap<>();
                map.put("port", deviceControlNode.getPort());
                map.put("controlDeviceNode", deviceControlNode.getControlDeviceNode());
                map.put("deviceId",deviceControlNode.getDeviceId());
                map.put("nodeId",deviceControlNode.getNodeId());
                map.put("nodeDescribe",deviceControlNode.getNodeDescribe());
                cacheInfo.put(deviceControlNode.getSid().toString(), map);
            });

            //判断是否开始下一轮
            if (list==null || list.size()<500) {
                break;
            }else {
                currentPage++;
            }

        }
        //4.将每个hash表的key单独作为索引存在cache的list集合内
        Set<String> node_index = cacheInfo.keySet();
        for (String nodeIndex : node_index) {

            cacheUtil.hPutAll("controlTable:" + nodeIndex, cacheInfo.get(nodeIndex));
            cacheUtil.lLeftPush("control_node_index", "controlTable:" + nodeIndex);
        }
    }
}
